package com.it2297.util;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Date;
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Multipart;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;

public class Email {
	private static final String username = "IT2297OOADPROJECT@gmail.com";
	private static final String password = "Keith12345";
//	private static final String username = "IT2297@hotmail.com";
//	private static final String password = "Keith12345";
	private static Properties properties = null;
	private static Session session = null;

	private static Email email = null;

	private Email() {
	}

	public static Email getInstance() {
		if (email == null) {
			setUp();
			email = new Email();
		}

		return email;
	}

	private static void setUp() {
		properties = new Properties();
		properties.put("mail.smtp.auth", "true");
		properties.put("mail.smtp.starttls.enable", "true");
		properties.put("mail.smtp.host", "smtp.gmail.com");
		properties.put("mail.smtp.port", "587");
	//	properties.put("mail.smtp.host", "smtp.live.com");
	//	properties.put("mail.smtp.port", "25");
		session = Session.getInstance(properties, new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		});
	}
	

	public boolean send(String from, String to, String subject, String text) {

		try {
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(from));
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
			message.setSubject(subject);
			message.setText(text);

			Transport.send(message);
			
			return true;

		} catch (MessagingException e) {
			return false;
			//throw new RuntimeException(e);
		}
	}
	/*
	public boolean sendImg(String from, String to , String subject, Image img){
		 
	        try {
	        Message message = new MimeMessage(session);
	        message.setFrom(new InternetAddress(from));
	        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
			message.setSubject(subject);
			
			Multipart multipart = new MimeMultipart("related");
			
			BodyPart messageBodyPart;
			messageBodyPart = new MimeBodyPart();
			messageBodyPart.setText("Hello"); // msgbody contains the contents of the html file
			messageBodyPart.setHeader("Content-Type", "text/html");
			multipart.addBodyPart(messageBodyPart);
			
			StringBuffer content = new StringBuffer();
	        content.append(img);
	        String str = content.toString();
	        DataSource source;
	        File file = new File(str);
	        if(file.exists()){
	        	messageBodyPart = new MimeBodyPart();
	        	source = new FileDataSource(file);
	        	messageBodyPart.setDataHandler(new DataHandler(source));
	        	messageBodyPart.setFileName(file.getName());
				messageBodyPart.setHeader("Content-ID", "<BarcodeImage>");
				//messageBodyPart.setDisposition("inline");
				//messageBodyPart.setDisposition(MimeBodyPart.INLINE);
				multipart.addBodyPart(messageBodyPart);
	        }
	        message.setContent(multipart);
	        Transport.send(message);
	        return true;
			
				
			} catch (MessagingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
			
	}  
	*/
}
